<?php

declare(strict_types=1);
namespace Service\StorageService;


use Service\BaseService;
use Service\Code\Normal;
use App\Model\BbcModel\StorageBondedOrder;
use App\Model\BbcModel\StorageBondedOrderDetail;
use App\Model\BbcModel\Goods;
use App\Model\BbcModel\PdaStockLoc;
use App\Model\BbcModel\GoodsBatch;

class StorageBondedOrderService extends BaseService
{
    /**
     * 获取入库单批次详细信息
     * @param type $cid
     * @param type $mn_sid
     * @param type $param
     * @return type
     */
    public function getInfoBySn($cid,$mn_sid,$param) {
        if(empty($cid)||empty($mn_sid)||empty($param['sn'])){
            return Normal::returnCode(Normal::FAIL, '参数必传');
        }
        $sn=$param['sn'];
        $orderinfo= StorageBondedOrder::instance()->info(['sn'=>$sn,'cid'=>$cid,'mn_sid'=>$mn_sid]);
        if(empty($orderinfo)){
            return Normal::returnCode(Normal::FAIL, '未找到相关单据');
        }
        $detailWhere=[
            'storage_bonded_order_id'=>$orderinfo['id']
        ];
        if(isset($param['barcode'])&&!empty($param['barcode'])){
            $detailWhere['barcode']=$param['barcode'];
        }
        if(isset($param['limit'])&&isset($param['page'])&&$param['limit']>0&&$param['page']>0){
            $orderdetail= StorageBondedOrderDetail::instance()->listPages($detailWhere,$param['limit'],$param['page']);
        }else{
            $orderdetail= StorageBondedOrderDetail::instance()->lists($detailWhere);
        }
        if(empty($orderdetail)){
            return Normal::returnCode(Normal::FAIL, '未找到相关单据详情');
        }
        $total=StorageBondedOrderDetail::instance()->countNum($detailWhere);
        $orderinfo['total']=$total;
        $skucodes= array_column($orderdetail, 'skucode');
        $goods= Goods::instance()->where('cid',$cid)->where('df',0)->whereIn('skucode',$skucodes)->get()->toArray();
        $goodsMaps= array_column($goods, null,'skucode');
        $orderinfo['wait_quantity']=bcsub((string)$orderinfo['quantity'], (string)$orderinfo['receipt_quantity']);
        $orderinfo['wait_goods_quantity']=bcsub((string)$orderinfo['goods_quantity'], (string)$orderinfo['receipt_goods_quantity']);
        if(isset($param['storage_loc'])&&$param['storage_loc']==1){
            $stock_loc= PdaStockLoc::instance()->lists([
                'df'=>0,
                'status'=>1,
                'cid'=>$cid,
                'mn_sid'=>$mn_sid
            ]);
            $stock_loc_map= array_column($stock_loc, null,'id');
        }
        foreach ($orderdetail as $key => &$item) {
            $item['goods_name']=$goodsMaps[$item['skucode']]['name']??$item['goods_name'];
            $item['img']=$goodsMaps[$item['skucode']]['img']??'';
            $item['bonded']=isset($goodsMaps[$item['skucode']]['is_bonded'])?Goods::$is_bonded[$goodsMaps[$item['skucode']]['is_bonded']]:'';
            $item['wait_quantity']=bcsub((string)$item['quantity'], (string)$item['receipt_quantity']);
            $item['batch']=$item['expired_date'];
            if(isset($param['storage_loc'])&&$param['storage_loc']==1){
                $goodsbatch= GoodsBatch::instance()->lists([
                    'goods_id'=>$goodsMaps[$item['skucode']]['id']??0,
                    'cid'=>$cid,
                    'mn_sid'=>$mn_sid,
                    'type'=>1
                ]);
                $batch_stock_loc= array_column($goodsbatch, 'stock_loc');
                $batch_stock_loc= array_unique($batch_stock_loc);
                $stock_loc_string=[];
                foreach ($batch_stock_loc as $k => $v) {
                    if($v>0&isset($stock_loc_map[$v])){
                        $stock_loc_string[]=$stock_loc_map[$v]['code'];
                    }
                }
                $item['stock_loc']= implode(',', $stock_loc_string);
            }
            unset($item);
        }
        $orderinfo['detail']=$orderdetail;
        return Normal::returnCode(Normal::SUC,'成功',$orderinfo);
    }
}
